package com.project.service.impl;

import com.project.bean.CutPageBean;
import com.project.bean.RoomBean;
import com.project.mapper.IRoomMapper;
import com.project.service.IRoomService;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class RoomServiceImpl extends BaseService implements IRoomService {

    @Override
    public void add(RoomBean room) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        mapper.add(room);
        session.commit();
        session.close();
    }

    @Override
    public boolean del(Integer roomId) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        if (mapper.getById(roomId).getInCount() > 0) {
            session.close();
            return false;
        }
        mapper.del(roomId);
        session.commit();
        session.close();
        return true;
    }

    @Override
    public List<RoomBean> getByGender(String gender) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        List<RoomBean> list = mapper.getByGender(gender);
        session.close();
        return list;
    }

    @Override
    public RoomBean getById(Integer roomId) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        RoomBean room = mapper.getById(roomId);
        session.close();
        return room;
    }

    @Override
    public CutPageBean<RoomBean> cutByItem(Integer pageNO, String addr, String status, String enable, String type) {
        CutPageBean<RoomBean> cutPageBean = new CutPageBean<>();
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        List<RoomBean> list = mapper.cutByItemList((pageNO - 1) * CutPageBean.PAGESIZE, CutPageBean.PAGESIZE, addr, status, enable, type);
        cutPageBean.setList(list);
        cutPageBean.setCount(mapper.getTotalByItem(addr, status, enable, type));
        session.close();
        return cutPageBean;
    }
}
